var fundTypeMap={};//基金代码和描述映射
var hospCode={}; //获取院区信息
$(function(){
		var fundType = diction.FUND_TYPE;
		var insType = new Array();
		if(typeof(fundType)!="undefined"){
			for(var m=0;m<fundType.length;m++){
				fundTypeMap[fundType[m].DICVALUE]=fundType[m].DICDESC;
				insType.push(fundType[m].DICVALUE);
			}
		}
		loadHCInfo();
})

/**
 * 
 * @ Discription:获取医保基金
 * @Autor ：liuhongjun
 * @returns
 * any
 * fundSetUp.js
 * 2017年7月21日下午4:57:33
 */
function loadHCInfo(){
	$.ajax({
		url : contextPath+"/Department/findAllHosp.dhc",
		async : false,
		data : {},
		type : "POST",
		success : function(result) {
			hospCode=result.data;
		}
	});
	//获取医保基金类型数据
	$.post(contextPath+"/systemConfig/getConfigByCode.dhc",{alterParam: "MED_INS_TYPE"}).done(function(result){
		var medInsData = result.data;// 基金类型数据
		if(medInsData && medInsData.length>0){
			
			var medInsArr  =medInsArr = medInsData.split("θ")==null?[]:medInsData.split("θ");// 基金类型数组
			var insType = {};// 基金类型数组
			//基金类型单选框赋值
			var insTypeStr="";
			for(var i=0;i<medInsArr.length;i++){
				insType[medInsArr[i].split("#")[0]]=medInsArr[i].split("#")[0];
				insTypeStr+="<option value="+medInsArr[i].split("#")[0]+">"+fundTypeMap[medInsArr[i].split("#")[0]]+"</option>";
			}
			$("#"+tabId+" #insetType").html(insTypeStr);
			
			//加载设置的主界面
			loadFundSet($("#"+tabId+" #insetType").val());
			$("#"+tabId+" #insetType").on("change",function(){
				loadFundSet($("#"+tabId+" #insetType").val());
			});
			
		}else{
			$("#"+tabId+" #totalFund_content").html("<div style='width: 100px;text-align: center; margin: auto;color: red;'>未配置基金类型</div>")
            drgUI.error('请先去系统配置模块配置基金类型!');
			return;
		}
	});
}
//加载全院医保总基金主页面
function loadFundSet(o){
		$("#"+tabId+" #totalFund_content").html("");
		var  newDate = new Date();
	    var nowYear = +newDate.getFullYear();
    
	    var str2 = '';
		str2 += "<div id='totalFundSet' >";
		str2 +="<div id='medicalUISet' >";
		str2 +="<div style='width:850px;'>";
		str2 +="<div class='panel panel-success'>";
		str2 +="<div class='panel-heading'>";
		str2 +="<h3 class='panel-title'>医保总基金配置</h3>";
		str2 +="</div>";
		str2 +="<div class='panel-body' style='background-color:white !important;border-color:white !important;'>";
		str2 +="<label for='HCTotalFundHospSet'>院区：</label>";
		str2 +="<select class='form-control' id='HCTotalFundHospSet' onchange='fundHospChange()' style='width:100px;display: inline-block; margin-right: 10px;'>";
		for(var i in hospCode){
			var hosp=hospCode[i];
			if(i=='0'){
				str2 +="<option value='"+(hosp.HOSP_CODE)+"'  selected>"+(hosp.HOSP_NAME)+"</option>";
			}else{
				str2 +="<option value='"+(hosp.HOSP_CODE)+"'>"+(hosp.HOSP_NAME)+"</option>";
			}
		}
		str2 +=" </select>";
		str2 +="<label for='HCTotalFundYearSet'>年份：</label>";
		str2 +="<select class='form-control' id='HCTotalFundYearSet' onchange='fundYearChange()' style='width:100px;display: inline-block; margin-right: 10px;'>";
		str2 +="<option value='"+(nowYear+1)+"'>"+(nowYear+1)+"</option>";
		str2 +="<option value='"+(nowYear)+"'  selected>"+(nowYear)+"</option>";
		str2 +="<option value='"+(nowYear-1)+"'>"+(nowYear-1)+"</option>";
		str2 +="<option value='"+(nowYear-2)+"'>"+(nowYear-2)+"</option>";
		str2 +="<option value='"+(nowYear-3)+"'>"+(nowYear-3)+"</option>";
		str2 +=" </select>";
		str2 +="<label for='HCTotalFundSet'>医保总基金：</label>";
		str2 +=" <input id='HCTotalFundSet' type='number' class='form-control' style='width:200px;display: inline-block;'/>元";
		str2 +="<button class='btn btn-primary pull-right' onclick='submitHCTotalFund()'>提&nbsp;&nbsp;交</button>";
		str2 +="</div>";
		str2 +="</div>";
		str2 +="<div class='panel panel-success'>";
		str2 +="<div class='panel-heading'>";
		str2 +="<h3 class='panel-title'>每月医保总额分配比例</h3>";
		str2 +=" </div>";
		str2 +="<div class='panel-body' style='background-color:white !important;border-color:white !important;' >";
		str2 +="<table id='monthFundTableSet' class='table table-bordered hc_table'>";
		str2 +="<thead><tr class='success'><td>年\月</td><td>1</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td>";
		str2 +="<td>7</td><td>8</td><td>9</td><td>10</td><td>11</td><td>12</td></tr>";
		str2 +="</thead>";
		str2 +="<tbody></tbody>";
		str2 +="</table>";
		str2 +="<a  class='pull-left' href='javascript:addLastYear(Set);' id='addLastYear'>添加上一年</a>";
		str2 +="<a  class='pull-left' href='javascript:addNextYear(Set);' id='addNextYear' style='margin-left: 20px;'>添加下一年</a>";
		str2 +="<a id='fundsubSet' class='btn btn-primary pull-right' onclick='submitMIMP()' >提&nbsp;&nbsp;交</a>";
		str2 +=" </div>";
		str2 +="</div>";
		str2 +=" </div>";
		str2 +="</div>"; 
		str2 += "</div>";
		$("#"+tabId+" #totalFund_content").html(str2);
	    
		getFundByType(o,"new");//获取医保总额每个月的分配比例
		getHCTotalFound(o,"new");//获取选中的年份的基金总额
};

//添加上一年
function addLastYear(o){
	var lastyear=$('#monthFundTableSet tr').first().attr('class');
	lastyear=parseInt(lastyear);
	if(!lastyear){
		lastyear=$("#monthFundTableSet tbody tr").first().find("td").html();
		if(typeof lastyear == "undefined" || lastyear == null || lastyear == ""){
			lastyear = new Date().getFullYear()-1;
		}else{
			lastyear=lastyear.substring(0,4);
		}
	}
	$("#monthFundTableSet tbody").prepend('<tr class="'+(lastyear-1)+'"><td>'+(lastyear-1)+'年</td>'
			+'<td class="01" ><input type="text"  maxlength="5" value="8.00" ><font style="">%</font></td>'
			+'<td class="02" ><input type="text"  maxlength="5" value="8.00" ><font style="padding-top:5px;">%</font></td>'
			+'<td class="03" style="padding:5px;"><input type="text"  maxlength="5" value="8.00"><font style="padding-top:5px;">%</font></td>'
			+'<td class="04" style="padding:5px;"><input type="text"  maxlength="5" value="8.00"><font style="padding-top:5px;">%</font></td>'
			+'<td class="05" style="padding:5px;"><input type="text"  maxlength="5" value="8.00" ><font style="padding-top:5px;">%</font></td>'
			+'<td class="06" style="padding:5px;"><input type="text"  maxlength="5" value="8.00" ><font style="padding-top:5px;">%</font></td>'
			+'<td class="07" style="padding:5px;"><input type="text"  maxlength="5" value="8.00" ><font style="padding-top:5px;">%</font></td>'
			+'<td class="08" style="padding:5px;"><input type="text"  maxlength="5" value="8.00" ><font style="padding-top:5px;">%</font></td>'
			+'<td class="09" style="padding:5px;"><input type="text"  maxlength="5" value="8.00" ><font style="padding-top:5px;">%</font></td>'
			+'<td class="10" style="padding:5px;"><input type="text"  maxlength="5" value="8.00" ><font style="padding-top:5px;">%</font></td>'
			+'<td class="11" style="padding:5px;"><input type="text"  maxlength="5" value="10.00" ><font style="padding-top:5px;">%</font></td>'
			+'<td class="12" style="padding:5px;"><input type="text"  maxlength="5" value="10.00" ><font style="padding-top:5px;">%</font></td>'
			);
}
//添加一行
function addNextYear(o){
	var lastyear=$('#monthFundTableSet tr').last().attr('class');
	lastyear=parseInt(lastyear);
	if(!lastyear){
		lastyear = new Date().getFullYear()-1;
	}
	$("#monthFundTableSet tbody").append('<tr class="'+(1+lastyear)+'"><td>'+(1+lastyear)+'年</td>'
			+'<td class="01" ><input type="text"  maxlength="5" value="8.00" ><font style="">%</font></td>'
			+'<td class="02" ><input type="text"  maxlength="5" value="8.00" ><font style="padding-top:5px;">%</font></td>'
			+'<td class="03" style="padding:5px;"><input type="text"  maxlength="5" value="8.00"><font style="padding-top:5px;">%</font></td>'
			+'<td class="04" style="padding:5px;"><input type="text"  maxlength="5" value="8.00"><font style="padding-top:5px;">%</font></td>'
			+'<td class="05" style="padding:5px;"><input type="text"  maxlength="5" value="8.00" ><font style="padding-top:5px;">%</font></td>'
			+'<td class="06" style="padding:5px;"><input type="text"  maxlength="5" value="8.00" ><font style="padding-top:5px;">%</font></td>'
			+'<td class="07" style="padding:5px;"><input type="text"  maxlength="5" value="8.00" ><font style="padding-top:5px;">%</font></td>'
			+'<td class="08" style="padding:5px;"><input type="text"  maxlength="5" value="8.00" ><font style="padding-top:5px;">%</font></td>'
			+'<td class="09" style="padding:5px;"><input type="text"  maxlength="5" value="8.00" ><font style="padding-top:5px;">%</font></td>'
			+'<td class="10" style="padding:5px;"><input type="text"  maxlength="5" value="8.00" ><font style="padding-top:5px;">%</font></td>'
			+'<td class="11" style="padding:5px;"><input type="text"  maxlength="5" value="10.00" ><font style="padding-top:5px;">%</font></td>'
			+'<td class="12" style="padding:5px;"><input type="text"  maxlength="5" value="10.00" ><font style="padding-top:5px;">%</font></td>'
			);
}

//表格列出医保总额每月分配比例
function getFundByType(o,hospCodeNew){
	
	if(hospCodeNew=="new"){
		hospCodeNew=hospCode[0].HOSP_CODE;
	}
   $.post(contextPath +'/MiMonthProPortion/selectMiMonthProByType.dhc',{"fundType":o,"hospCode":hospCodeNew},function(result){
	var data = result.data;
	if( data && data.length>0){
		var tbodyStr='';
		var y='';
		var j=0;
		$.each(data,function(i,n){
			
			var year=n.YEAR;//截取年份
			var month=n.MONTH;//截取月份
			var value=n.MIMPVALUE;
			if(year!=y){
				if(y!=''){
					while (j<13) {
						tbodyStr+='<td style="color:#aaa;">无</td>';
						j++;
					}
					tbodyStr+='</tr>';
				}
				tbodyStr+='<tr class="'+year+'"><td>'+year+'年</td>';
				y=year;
				j=1;
			}
			
			//循环，绘制表格
			while (j<13) {
				if (j==month) {
					//早于本月之前的不可编辑
					if(year>=new Date().getFullYear()&&month>=(new Date().getMonth()+1)){
						tbodyStr+='<td class="'+month+'" style="padding:5px;"><input type="text" value="'
									+(value*100).toFixed(2)+'" maxlength="5"><font style="padding-top:5px;">%</font></td>';
					}else if(year>new Date().getFullYear()){
						tbodyStr+='<td class="'+month+'" style="padding:5px;"><input type="text" value="'
									+(value*100).toFixed(2)+'" maxlength="5"><font style="padding-top:5px;">%</font></td>';
						
					}else {
						tbodyStr+='<td class="'+month+'"  style="color:#aaa;">'+(value*100).toFixed(2)+'%</td>';
					}
					j++;
					break;
				}else {
					//将没有值的月份置为空
					if(year>=new Date().getFullYear()){
						tbodyStr+='<td class="0'+j+'" style="padding:5px;"><input type="text"'
						+' maxlength="5"><font style="padding-top:5px;">%</font></td>';
					}else {
						tbodyStr+='<td style="color:#aaa;">无</td>';
					}
					j++;
				}
			}
		});
		for(j;j<13;j++){
			if (j<10) {
				tbodyStr+='<td class="0'+j+'" style="padding:5px;"><input type="text"'
					+' maxlength="5"><font style="padding-top:5px;">%</font></td>';
			}else {
				tbodyStr+='<td class="'+j+'" style="padding:5px;"><input type="text"'
				+' maxlength="5"><font style="padding-top:5px;">%</font></td>';
			}
			
		}
		tbodyStr+='</tr>';
		$("#monthFundTableSet tbody").html(tbodyStr);
	}else{
		$("#monthFundTableSet tbody").html("");
	}
 })
};


//院区变化，对应的输入框内的基金总额变化
function fundHospChange(){
	var fundValue = $("#"+tabId+" #insetType").val();
	var hospCodeNew=$('#'+tabId+' #HCTotalFundHospSet').val();
	getHCTotalFound(fundValue,hospCodeNew);
	getFundByType(fundValue,hospCodeNew);
}

//年份变化，对应的输入框内的基金总额变化
function fundYearChange(){
	var fundValue = $("#"+tabId+" #insetType").val();
	var hospCodeNew=$('#'+tabId+' #HCTotalFundHospSet').val();
	getHCTotalFound(fundValue,hospCodeNew);
}

//获取某一年的基金总额
function getHCTotalFound(o,hospCodeNew){
	if(hospCodeNew=="new"){
		hospCodeNew=hospCode[0].HOSP_CODE;
	}
	var HCTotalFundYear = $("#HCTotalFundYearSet").val();
	$.post(contextPath +'/MiMonthProPortion/getFundByType.dhc',
			{
			'HCTotalFundYear':HCTotalFundYear,
			'fundType':o,
			'hospCode':hospCodeNew
			}
	).done(function(result){
		var data = result.data;
		if(data[0]!=undefined){
			 $("#HCTotalFundSet").val(data[0].MEDINS_FUND_VALUE);
		}else{
			 $("#HCTotalFundSet").val(null);
		}
	});

}

//提交医保总基金的数据
//o表示触发这个事件的对象
function submitHCTotalFund(){
	var HCTotalFundYear = $("#HCTotalFundYearSet").val();
	var HCTotalFund = $("#HCTotalFundSet").val();
	var hospCodeSum = $("#HCTotalFundHospSet").val();
	var hospCodeAll=hospCodeSum.split(",").length-1;
	if(hospCodeAll>0){
		 drgUI.warning('选择院区数据过多，更改配置时请选择单一院区');
		 return;
	}
	if (HCTotalFund == null || HCTotalFund == undefined || HCTotalFund == '') { 
    	 drgUI.error('数据为空，提交失败');
    	 return;
	} 
	var re=/^\d*\.{0,2}\d{0,2}$/;
	if (re.test(HCTotalFund)) {
		$.post(contextPath +'/MiMonthProPortion/addHCTotalFundByType.dhc',
				{
				'HCTotalFundYear':HCTotalFundYear,
				'HCTotalFund':HCTotalFund,
				'fundType': $("#"+tabId+" #insetType").val(),
				'hospCode':hospCodeSum
				}
		).done(function(){
		    	drgUI.success('提交成功!');
		});
	} else {
    	drgUI.warning('数据有误，提交失败!');
	}
}

//提交每月分配额度的数据
function submitMIMP(){
	var fundValue = $("#"+tabId+" #insetType").val();
	var hospCodeSum = $("#HCTotalFundHospSet").val();
	var jsonStr='';
	var i=0;
	var j=0;
	var flag = 0;
	//拼接字符串
	$('#monthFundTableSet input').each(function(){
		var year=$(this).parent().parent().attr('class');
		var month=$(this).parent().attr('class');
		var re=/^\d*\.{0,2}\d{0,2}$/;
		var inputVal = $(this).val();
		if (inputVal == null || inputVal == undefined || inputVal == '') { 
			inputVal = 0;
		} 
		if (!re.test($(this).val()) ) 
		{ 
			flag = 1;
			return;
		}
		var value= (parseFloat($(this).val())/100).toFixed(4);
	    jsonStr+='\"'+year+month+'\":'+value;
	    j++;
	   
			jsonStr+=',';
		
	});
	jsonStr = jsonStr.substring(0,jsonStr.length-1);
	if(flag == 1){
    	drgUI.warning('数据有误1，提交失败!');
		return ;
	}
	
	$('#monthFundTableSet tbody tr').each(function(i,n){
		 var obj = $(n);
		 var yearflag;
		 var sumMonthPercent;
		 var year = obj.attr('class');
		 if(year >=new Date().getFullYear()){
			 if(year!=yearflag){
				 yearflag =year;
				 sumMonthPercent = 0;
			 }
			 $(this).children('td').each(function(i,v){
				 var monthObj =$(v);
				 var month = monthObj.attr('class');
				 if(month != null && month != undefined && month != ''){
					 if(year ==new Date().getFullYear() && month<(new Date().getMonth()+1)){
						 var monthper = monthObj.text();
						 sumMonthPercent = sumMonthPercent + parseFloat(monthper.split("%")[0]);
					 }
					 else{
						var inputVal = $(this).children('input').val();
						if (inputVal == null || inputVal == undefined || inputVal == '') { 
							inputVal = 0;
						} 
						sumMonthPercent = sumMonthPercent +(parseFloat(inputVal));
					 }
				 }
				 
			 });
			 if(sumMonthPercent > 100){
				 flag = year
			 }
		 }
			
	});
	
	if(flag > 1){
    	drgUI.warning(flag+'年医保分配比例大于100%，请重新分配');
		return ;
	}
	
	if (jsonStr.length>2) {
		$.post(contextPath +'/MiMonthProPortion/addMonthProporByType.dhc',{
		'jsonStr':jsonStr,'fundType':fundValue,'hospCode':hospCodeSum}).done(function(){
		    	drgUI.success('提交成功');
		});
	} else {
  	    drgUI.error('数据未修改，提交失败');
	}
}


